package com.sharer.last.controller;

import com.sharer.last.annotation.OperationLogger;
import com.sharer.last.base.constant.CommonConstant;
import com.sharer.last.base.model.PageVO;
import com.sharer.last.base.model.R;
import com.sharer.last.interfaces.ValidateGroup;
import com.sharer.last.model.dto.blog.BlogChangeDTO;
import com.sharer.last.model.dto.blog.BlogQueryDTO;
import com.sharer.last.model.dto.blog.BlogVisibilityDTO;
import com.sharer.last.model.vo.blog.BlogIdAndTitleVO;
import com.sharer.last.model.vo.blog.BlogVO;
import com.sharer.last.model.vo.category.CategoryAndTagVO;
import com.sharer.last.service.BlogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 博客文章后台管理
 *
 * @ClassName BlogAdminController
 * @Author wangjin
 * @Date 2023/5/5 21:03
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping(CommonConstant.BLOG_ADMIN_API + "/blog")
public class BlogAdminController {

    @Resource
    BlogService blogService;

    /**
     * 按id获取博客详情
     *
     * @param id 博客id
     * @return
     */
    @GetMapping("/findById/{id}")
    public R<BlogVO> findById(@PathVariable("id") Long id) {
        BlogVO blog = blogService.findById(id, true);
        return R.success(blog);
    }

    /**
     * 获取分类、标签数据字典
     *
     * @return
     */
    @GetMapping("/findBlogRelatedData")
    public R<CategoryAndTagVO> findBlogRelatedData() {
        CategoryAndTagVO categoryAndTagVO = blogService.findBlogRelatedData();
        return R.success(categoryAndTagVO);
    }

    /**
     * 分页查询
     *
     * @param queryDTO
     * @return
     */
    @PostMapping("/page")
    public R<PageVO<BlogVO>> page(@RequestBody @Valid BlogQueryDTO queryDTO) {
        PageVO<BlogVO> list = blogService.selectListByPage(queryDTO);
        return R.success(list);
    }

    /**
     * 获取所有博客 只需要id和博客标题
     *
     * @return
     */
    @GetMapping("/selectSimpleList")
    public R<List<BlogIdAndTitleVO>> selectSimpleList() {
        List<BlogIdAndTitleVO> list = blogService.selectSimpleList();
        return R.success(list);
    }

    /**
     * 保存草稿或发布新文章
     *
     * @param dto 博客文章DTO
     * @return
     */
    @OperationLogger("发布博客")
    @PostMapping("/create")
    public R create(@RequestBody @Validated(ValidateGroup.InsertGroup.class) BlogChangeDTO dto) {
        blogService.createBlog(dto);
        return R.handle();
    }

    /**
     * 更新博客
     *
     * @param dto 博客文章DTO
     * @return
     */
    @OperationLogger("更新博客")
    @PutMapping("/update")
    public R update(@RequestBody @Validated(ValidateGroup.UpdateGroup.class) BlogChangeDTO dto) {
        blogService.updateBlog(dto);
        return R.handle();
    }

    /**
     * 更新博客置顶状态
     *
     * @return
     */
    @OperationLogger("更新博客置顶状态")
    @PutMapping("/updateTop")
    public R updateTop(@RequestBody BlogVisibilityDTO blogVisibilityDTO) {
        blogService.updateBlogTopById(blogVisibilityDTO.getId(), blogVisibilityDTO.getIsTop());
        return R.handle();
    }

    /**
     * 更新博客推荐状态
     *
     * @return
     */
    @OperationLogger("更新博客推荐状态")
    @PutMapping("/updateRecommend")
    public R updateRecommend(@RequestBody BlogVisibilityDTO blogVisibilityDTO) {
        blogService.updateBlogRecommendById(blogVisibilityDTO.getId(), blogVisibilityDTO.getIsRecommend());
        return R.handle();
    }

    /**
     * 更新博客可见性状态
     *
     * @param blogVisibility 博客可见性DTO
     * @return
     */
    @OperationLogger("更新博客可见性状态")
    @PutMapping("/visibility")
    public R updateVisibility(@RequestBody @Validated BlogVisibilityDTO blogVisibility) {
        blogService.updateBlogVisibilityById(blogVisibility);
        return R.handle();
    }

    /**
     * 删除博客文章、删除博客文章下的所有评论、同时维护 blog_tag 表
     *
     * @param id 文章id
     * @return
     */
    @OperationLogger("删除博客")
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id) {
        blogService.removeBlogById(id);
        return R.handle();
    }
}
